package com.nwpu.coursesupport.service.imple;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.Status;
import com.nwpu.coursesupport.enumeration.TaskType;
import com.nwpu.coursesupport.mapper.LessonMapper;
import com.nwpu.coursesupport.service.AbstractService;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.service.param.TaskServiceParam.CreateLessonTaskParam;
import com.nwpu.coursesupport.tool.ServiceUtil;
import com.nwpu.coursesupport.view.LessonView;
import com.nwpu.coursesupport.view.ShareDetailView;
import com.nwpu.coursesupport.view.ShareTaskView;
import com.nwpu.coursesupport.view.criteria.ShareDetailSearchCriteria;
import com.nwpu.coursesupport.view.criteria.ShareTaskSearchCriteria;
import com.nwpu.coursesupport.view.mapper.ShareTaskViewMapper;

@Service
public class ShareTaskViewService extends AbstractService{
	@Autowired
	private ShareTaskViewMapper shareTaskViewMapper;
	
	@Autowired
	private LessonMapper lessonMapper;
	
    protected static Logger logger = LoggerFactory.getLogger(ShareTaskViewService.class);

	
	public JSONResult list(IDParam p){
		JSONResult rst=new JSONResult();
		rst.fail();
		if(p==null||p.getId()==null){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		LessonView view=this.lessonMapper.findViewById(p.getId());
		if(view==null){
			logger.error("该课程不存在");
			return rst.fail("该课程不存在");
		}
		ShareTaskSearchCriteria sc =new ShareTaskSearchCriteria();
		sc.setIsShare(Status.YES);
		sc.setIsVisible(Status.YES);
		sc.setType(TaskType.TASKMAIN);
		sc.setLessonId(p.getId());
		List<ShareTaskView> list=this.shareTaskViewMapper.shareList(sc);
		
		rst.addData("list",list);	
		rst.addData("lesson", view);
		rst.success();
		return rst;
	}
	
	public JSONResult ajaxTList(CreateLessonTaskParam p){
		JSONResult rst = new JSONResult();
		rst.fail();
		if(p == null || p.getLessonId() < 0){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		LessonView view = this.lessonMapper.findViewById(p.getLessonId());
		if(view == null){
			logger.error("该课程不存在");
			return rst.fail("该课程不存在");
		}
		ShareTaskSearchCriteria sc = new ShareTaskSearchCriteria();
		sc.setIsShare(Status.YES);
		sc.setIsVisible(Status.YES);
		if(p.getType() == null){
			sc.setType(TaskType.TASKMAIN);
			sc.setSubType(null);
		}else{
			sc.setSubType(p.getType());
			sc.setType(null);
		}
		
		sc.setLessonId(p.getLessonId());
		
		List<ShareTaskView> list=this.shareTaskViewMapper.shareList(sc);
		
		rst.addData("list",list);	
		rst.addData("lesson", view);
		rst.success();
		return rst;
	}
	
	public JSONResult detail(IDParam p){
		JSONResult rst=new JSONResult();
		rst.fail();
		if(p==null||p.getId()==null){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		ShareDetailSearchCriteria sc=new ShareDetailSearchCriteria();
		sc.setId(p.getId());
		sc.setIsShare(Status.YES);
		sc.setIsVisible(Status.YES);
		sc.setType(TaskType.TASKMAIN);
		ShareDetailView detail=this.shareTaskViewMapper.shareTaskDetail(sc);
		if(detail==null){
			logger.error("该共享结果不存在");
			return rst.fail("该共享结果不存在");
		}
		if(detail.getType() == null){
			detail.setType(sc.getType());
		}
		
		detail.setAnswerAttachment(ServiceUtil.DeserializeFiles(detail.getAnswerAttachmentStr()));
		
		rst.addData("view", detail);
		rst.success();
		return rst;
	}
}
